//
//  NSString+Safe.m
//  CommonTool
//
//  Created by Name i on 2022/4/14.
//

#import "NSString+Safe.h"

@implementation NSString (Safe)

+ (void)exchangeMethod {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class stringClass = NSClassFromString(@"__NSCFConstantString");
        
        //characterAtIndex
        [NSObject tnSwizzlingInstanceMethod:@selector(characterAtIndex:)
                                 withMethod:@selector(safe_characterAtIndex:)
                                  withClass:stringClass];
        
        //substringFromIndex
        [NSObject tnSwizzlingInstanceMethod:@selector(substringFromIndex:)
                                 withMethod:@selector(safe_substringFromIndex:)
                                  withClass:stringClass];
        
        //substringToIndex
        [NSObject tnSwizzlingInstanceMethod:@selector(substringToIndex:)
                                 withMethod:@selector(safe_substringToIndex:)
                                  withClass:stringClass];
        
        //substringWithRange:
        [NSObject tnSwizzlingInstanceMethod:@selector(substringWithRange:)
                                 withMethod:@selector(safe_substringWithRange:)
                                  withClass:stringClass];
        
        //stringByReplacingOccurrencesOfString:
        [NSObject tnSwizzlingInstanceMethod:@selector(stringByReplacingOccurrencesOfString:withString:)
                                 withMethod:@selector(safe_stringByReplacingOccurrencesOfString:withString:)
                                  withClass:stringClass];
        
        //stringByReplacingOccurrencesOfString:withString:options:range:
        [NSObject tnSwizzlingInstanceMethod:@selector(stringByReplacingOccurrencesOfString:withString:options:range:)
                                 withMethod:@selector(safe_stringByReplacingOccurrencesOfString:withString:options:range:)
                                  withClass:stringClass];
        
        //stringByReplacingCharactersInRange:withString:
        [NSObject tnSwizzlingInstanceMethod:@selector(stringByReplacingCharactersInRange:withString:)
                                 withMethod:@selector(safe_stringByReplacingCharactersInRange:withString:)
                                  withClass:stringClass];
    });
}


- (unichar)safe_characterAtIndex:(NSUInteger)index {
    unichar characteristic;
    @try {
        characteristic = [self safe_characterAtIndex:index];
    }
    @catch (NSException *exception) {
        [NNDefender crashWithException:exception defaultToDo:@"    NNDefender default is to return a without assign unichar."];
    }
    @finally {
        return characteristic;
    }
}

- (NSString *)safe_substringFromIndex:(NSUInteger)from {
    NSString *subString = nil;
    @try {
        subString = [self safe_substringFromIndex:from];
    }
    @catch (NSException *exception) {
        [NNDefender crashWithException:exception defaultToDo:    NNDefenderDefaultReturnNil];
    }
    @finally {
        return subString;
    }
}

- (NSString *)safe_substringToIndex:(NSUInteger)to {
    NSString *subString = nil;
    @try {
        subString = [self safe_substringToIndex:to];
    }
    @catch (NSException *exception) {
        [NNDefender crashWithException:exception defaultToDo:    NNDefenderDefaultReturnNil];
    }
    @finally {
        return subString;
    }
}

- (NSString *)safe_substringWithRange:(NSRange)range {
    NSString *subString = nil;
    @try {
        subString = [self safe_substringWithRange:range];
    }
    @catch (NSException *exception) {
        [NNDefender crashWithException:exception defaultToDo:    NNDefenderDefaultReturnNil];
    }
    @finally {
        return subString;
    }
}

- (NSString *)safe_stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement {
    NSString *targetString = nil;
    @try {
        targetString = [self safe_stringByReplacingOccurrencesOfString:target withString:replacement];
    }
    @catch (NSException *exception) {
        [NNDefender crashWithException:exception defaultToDo:    NNDefenderDefaultReturnNil];
    }
    @finally {
        return targetString;
    }
}

- (NSString *)safe_stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)options range:(NSRange)searchRange {
    
    NSString *targetString = nil;
    @try {
        targetString = [self safe_stringByReplacingOccurrencesOfString:target withString:replacement options:options range:searchRange];
    }
    @catch (NSException *exception) {
        [NNDefender crashWithException:exception defaultToDo:    NNDefenderDefaultReturnNil];
    }
    @finally {
        return targetString;
    }
}

- (NSString *)safe_stringByReplacingCharactersInRange:(NSRange)range withString:(NSString *)replacement {
    NSString *targetString = nil;
    @try {
        targetString = [self safe_stringByReplacingCharactersInRange:range withString:replacement];
    }
    @catch (NSException *exception) {
        [NNDefender crashWithException:exception defaultToDo:    NNDefenderDefaultReturnNil];
    }
    @finally {
        return targetString;
    }
}



@end
